package com.example.service.serviceImple;

import com.example.mapper.one.GraduateMapper;
import com.example.mapper.one.ThesisMapper;
import com.example.mapper.one.TreeMapper;
import com.example.mapper.two.Neo4j;
import com.example.pojo.ThesisInfo;
import com.example.service.ThesisService;
import com.hankcs.hanlp.HanLP;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ThesisServiceImple implements ThesisService {

    @Resource
    private ThesisMapper thesisMapper;

    @Resource
    private TreeMapper treeMapper;

    @Resource
    private Neo4j neo4j;

    @Override
    public int addThesisInfo(ThesisInfo thesisInfo) {
        int majorId = treeMapper.getAncestor((Integer) thesisInfo.getTeamId());
        int gradeId = treeMapper.getAncestor(majorId);
        thesisInfo.setGradeId(gradeId);
        thesisInfo.setMajorId(majorId);
        int isAdd = thesisMapper.addThesisInfo(thesisInfo);
        if(isAdd>0){
            isAdd = 0;
            System.out.println(thesisInfo.getKeywords());
            String[] keywordArr = thesisInfo.getKeywords().split(",");
            for(int i= 0;i < keywordArr.length; i++){
                int category = 1;
                if(i == 0){
                    category = 0;
                }
                isAdd += neo4j.addNode(keywordArr[i],50,thesisInfo.getMajorId(),category);
//                isAdd += neo4j

            }
            for(int j = 0;j < keywordArr.length-1; j++){
                isAdd += neo4j.addLink(keywordArr[j],keywordArr[j+1]);
            }
            System.out.println(isAdd);
        }
        return isAdd;
    }

    @Override
    public List getKeywords(Integer majorId) {
        List<String> keywordArr = thesisMapper.queryKeywords(majorId);
        String content = String.join("",keywordArr);
        List<String> keywordList = HanLP.extractKeyword(content, 78);
        return keywordList;
    }

}
